<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Zend_CodeGenerator Examples - Zend Framework Manual</title>

    <link href="../css/shCore.css" rel="stylesheet" type="text/css" />
    <link href="../css/shThemeDefault.css" rel="stylesheet" type="text/css" />
    <link href="../css/styles.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Zend Framework</h1>
<h2>Programmer's Reference Guide</h2>
<ul>
    <li><a href="../en/zend.codegenerator.examples.html">Inglês (English)</a></li>
    <li><a href="../pt-br/zend.codegenerator.examples.html">Português Brasileiro (Brazilian Portuguese)</a></li>
</ul>
<table width="100%">
    <tr valign="top">
        <td width="85%">
            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.codegenerator.introduction.html">Introduction</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.codegenerator.html">Zend_CodeGenerator</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.codegenerator.reference.html">Zend_CodeGenerator Reference</a></div>
                    </td>
                </tr>
            </table>
<hr />
<div id="zend.codegenerator.examples" class="section"><div class="info"><h1 class="title">Zend_CodeGenerator Examples</h1></div>
    

    <div class="example" id="zend.codegenerator.examples.class"><div class="info"><p><b>Example #1 Generating PHP classes</b></p></div>
        

        <div class="example-contents"><p>
            The following example generates an empty class with a class-level
            DocBlock.
        </p></div>

        <pre class="programlisting brush: php">
$foo      = new Zend_CodeGenerator_Php_Class();
$docblock = new Zend_CodeGenerator_Php_Docblock(array(
    &#039;shortDescription&#039; =&gt; &#039;Sample generated class&#039;,
    &#039;longDescription&#039;  =&gt; &#039;This is a class generated with Zend_CodeGenerator.&#039;,
    &#039;tags&#039;             =&gt; array(
        array(
            &#039;name&#039;        =&gt; &#039;version&#039;,
            &#039;description&#039; =&gt; &#039;$Rev:$&#039;,
        ),
        array(
            &#039;name&#039;        =&gt; &#039;license&#039;,
            &#039;description&#039; =&gt; &#039;New BSD&#039;,
        ),
    ),
));
$foo-&gt;setName(&#039;Foo&#039;)
    -&gt;setDocblock($docblock);
echo $foo-&gt;generate();
</pre>


        <div class="example-contents"><p>
            The above code will result in the following:
        </p></div>

        <pre class="programlisting brush: php">
/**
 * Sample generated class
 *
 * This is a class generated with Zend_CodeGenerator.
 *
 * @version $Rev:$
 * @license New BSD
 *
 */
class Foo
{

}
</pre>

    </div>

    <div class="example" id="zend.codegenerator.examples.class-properties"><div class="info"><p><b>Example #2 Generating PHP classes with class properties</b></p></div>
        

        <div class="example-contents"><p>
            Building on the previous example, we now add properties to our
            generated class.
        </p></div>

        <pre class="programlisting brush: php">
$foo      = new Zend_CodeGenerator_Php_Class();
$docblock = new Zend_CodeGenerator_Php_Docblock(array(
    &#039;shortDescription&#039; =&gt; &#039;Sample generated class&#039;,
    &#039;longDescription&#039;  =&gt; &#039;This is a class generated with Zend_CodeGenerator.&#039;,
    &#039;tags&#039;             =&gt; array(
        array(
            &#039;name&#039;        =&gt; &#039;version&#039;,
            &#039;description&#039; =&gt; &#039;$Rev:$&#039;,
        ),
        array(
            &#039;name&#039;        =&gt; &#039;license&#039;,
            &#039;description&#039; =&gt; &#039;New BSD&#039;,
        ),
    ),
));
$foo-&gt;setName(&#039;Foo&#039;)
    -&gt;setDocblock($docblock)
    -&gt;setProperties(array(
        array(
            &#039;name&#039;         =&gt; &#039;_bar&#039;,
            &#039;visibility&#039;   =&gt; &#039;protected&#039;,
            &#039;defaultValue&#039; =&gt; &#039;baz&#039;,
        ),
        array(
            &#039;name&#039;         =&gt; &#039;baz&#039;,
            &#039;visibility&#039;   =&gt; &#039;public&#039;,
            &#039;defaultValue&#039; =&gt; &#039;bat&#039;,
        ),
        array(
            &#039;name&#039;         =&gt; &#039;bat&#039;,
            &#039;const&#039;        =&gt; true,
            &#039;defaultValue&#039; =&gt; &#039;foobarbazbat&#039;,
        ),
    ));
echo $foo-&gt;generate();
</pre>


        <div class="example-contents"><p>
            The above results in the following class definition:
        </p></div>

        <pre class="programlisting brush: php">
/**
 * Sample generated class
 *
 * This is a class generated with Zend_CodeGenerator.
 *
 * @version $Rev:$
 * @license New BSD
 *
 */
class Foo
{

    protected $_bar = &#039;baz&#039;;

    public $baz = &#039;bat&#039;;

    const bat = &#039;foobarbazbat&#039;;

}
</pre>

    </div>

    <div class="example" id="zend.codegenerator.examples.class-methods"><div class="info"><p><b>Example #3 Generating PHP classes with class methods</b></p></div>
        

        <div class="example-contents"><p>
            <span class="classname">Zend_CodeGenerator_Php_Class</span> allows you to attach
            methods with optional content to your classes. Methods may be
            attached as either arrays or concrete
            <span class="classname">Zend_CodeGenerator_Php_Method</span> instances.
        </p></div>

        <pre class="programlisting brush: php">
$foo      = new Zend_CodeGenerator_Php_Class();
$docblock = new Zend_CodeGenerator_Php_Docblock(array(
    &#039;shortDescription&#039; =&gt; &#039;Sample generated class&#039;,
    &#039;longDescription&#039;  =&gt; &#039;This is a class generated with Zend_CodeGenerator.&#039;,
    &#039;tags&#039;             =&gt; array(
        array(
            &#039;name&#039;        =&gt; &#039;version&#039;,
            &#039;description&#039; =&gt; &#039;$Rev:$&#039;,
        ),
        array(
            &#039;name&#039;        =&gt; &#039;license&#039;,
            &#039;description&#039; =&gt; &#039;New BSD&#039;,
        ),
    ),
));
$foo-&gt;setName(&#039;Foo&#039;)
    -&gt;setDocblock($docblock)
    -&gt;setProperties(array(
        array(
            &#039;name&#039;         =&gt; &#039;_bar&#039;,
            &#039;visibility&#039;   =&gt; &#039;protected&#039;,
            &#039;defaultValue&#039; =&gt; &#039;baz&#039;,
        ),
        array(
            &#039;name&#039;         =&gt; &#039;baz&#039;,
            &#039;visibility&#039;   =&gt; &#039;public&#039;,
            &#039;defaultValue&#039; =&gt; &#039;bat&#039;,
        ),
        array(
            &#039;name&#039;         =&gt; &#039;bat&#039;,
            &#039;const&#039;        =&gt; true,
            &#039;defaultValue&#039; =&gt; &#039;foobarbazbat&#039;,
        ),
    ))
    -&gt;setMethods(array(
        // Method passed as array
        array(
            &#039;name&#039;       =&gt; &#039;setBar&#039;,
            &#039;parameters&#039; =&gt; array(
                array(&#039;name&#039; =&gt; &#039;bar&#039;),
            ),
            &#039;body&#039;       =&gt; &#039;$this-&gt;_bar = $bar;&#039; . &quot;\n&quot; . &#039;return $this;&#039;,
            &#039;docblock&#039;   =&gt; new Zend_CodeGenerator_Php_Docblock(array(
                &#039;shortDescription&#039; =&gt; &#039;Set the bar property&#039;,
                &#039;tags&#039;             =&gt; array(
                    new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
                        &#039;paramName&#039; =&gt; &#039;bar&#039;,
                        &#039;datatype&#039;  =&gt; &#039;string&#039;
                    )),
                    new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
                        &#039;datatype&#039;  =&gt; &#039;string&#039;,
                    )),
                ),
            )),
        ),
        // Method passed as concrete instance
        new Zend_CodeGenerator_Php_Method(array(
            &#039;name&#039; =&gt; &#039;getBar&#039;,
            &#039;body&#039;       =&gt; &#039;return $this-&gt;_bar;&#039;,
            &#039;docblock&#039;   =&gt; new Zend_CodeGenerator_Php_Docblock(array(
                &#039;shortDescription&#039; =&gt; &#039;Retrieve the bar property&#039;,
                &#039;tags&#039;             =&gt; array(
                    new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
                        &#039;datatype&#039;  =&gt; &#039;string|null&#039;,
                    )),
                ),
            )),
        )),
    ));

echo $foo-&gt;generate();
</pre>


        <div class="example-contents"><p>
            The above generates the following output:
        </p></div>

        <pre class="programlisting brush: php">
/**
 * Sample generated class
 *
 * This is a class generated with Zend_CodeGenerator.
 *
 * @version $Rev:$
 * @license New BSD
 */
class Foo
{

    protected $_bar = &#039;baz&#039;;

    public $baz = &#039;bat&#039;;

    const bat = &#039;foobarbazbat&#039;;

    /**
     * Set the bar property
     *
     * @param string bar
     * @return string
     */
    public function setBar($bar)
    {
        $this-&gt;_bar = $bar;
        return $this;
    }

    /**
     * Retrieve the bar property
     *
     * @return string|null
     */
    public function getBar()
    {
        return $this-&gt;_bar;
    }

}
</pre>

    </div>

    <div class="example" id="zend.codegenerator.examples.file"><div class="info"><p><b>Example #4 Generating PHP files</b></p></div>
        

        <div class="example-contents"><p>
            <span class="classname">Zend_CodeGenerator_Php_File</span> can be used to generate the
            contents of a <acronym class="acronym">PHP</acronym> file. You can include classes as well as arbitrary
            content body. When attaching classes, you should attach either
            concrete <span class="classname">Zend_CodeGenerator_Php_Class</span> instances or an
            array defining the class.
        </p></div>

        <div class="example-contents"><p>
            In the example below, we will assume you&#039;ve defined
            <var class="varname">$foo</var> per one of the class definitions in a previous
            example.
        </p></div>

        <pre class="programlisting brush: php">
$file = new Zend_CodeGenerator_Php_File(array(
    &#039;classes&#039;  =&gt; array($foo);
    &#039;docblock&#039; =&gt; new Zend_CodeGenerator_Php_Docblock(array(
        &#039;shortDescription&#039; =&gt; &#039;Foo class file&#039;,
        &#039;tags&#039;             =&gt; array(
            array(
                &#039;name&#039;        =&gt; &#039;license&#039;,
                &#039;description&#039; =&gt; &#039;New BSD&#039;,
            ),
        ),
    )),
    &#039;body&#039;     =&gt; &#039;define(\&#039;APPLICATION_ENV\&#039;, \&#039;testing\&#039;);&#039;,
));
</pre>


        <div class="example-contents"><p>
            Calling  <span class="methodname">generate()</span> will generate the code -- but not
            write it to a file. You will need to capture the contents and write
            them to a file yourself. As an example:
        </p></div>

        <pre class="programlisting brush: php">
$code = $file-&gt;generate();
file_put_contents(&#039;Foo.php&#039;, $code);
</pre>


        <div class="example-contents"><p>
            The above will generate the following file:
        </p></div>

        <pre class="programlisting brush: php">
&lt;?php
/**
 * Foo class file
 *
 * @license New BSD
 */

/**
 * Sample generated class
 *
 * This is a class generated with Zend_CodeGenerator.
 *
 * @version $Rev:$
 * @license New BSD
 */
class Foo
{

    protected $_bar = &#039;baz&#039;;

    public $baz = &#039;bat&#039;;

    const bat = &#039;foobarbazbat&#039;;

    /**
     * Set the bar property
     *
     * @param string bar
     * @return string
     */
    public function setBar($bar)
    {
        $this-&gt;_bar = $bar;
        return $this;
    }

    /**
     * Retrieve the bar property
     *
     * @return string|null
     */
    public function getBar()
    {
        return $this-&gt;_bar;
    }

}

define(&#039;APPLICATION_ENV&#039;, &#039;testing&#039;);
</pre>

    </div>

    <div class="example" id="zend.codegenerator.examples.reflection-file"><div class="info"><p><b>Example #5 Seeding PHP file code generation via reflection</b></p></div>
        

        <div class="example-contents"><p>
            You can add <acronym class="acronym">PHP</acronym> code to an existing <acronym class="acronym">PHP</acronym> file
            using the code generator. To do so, you need to first do reflection on it. The
            static method  <span class="methodname">fromReflectedFileName()</span> allows you to do
            this.
        </p></div>

        <pre class="programlisting brush: php">
$generator = Zend_CodeGenerator_Php_File::fromReflectedFileName($path);
$body = $generator-&gt;getBody();
$body .= &quot;\n\$foo-&gt;bar();&quot;;
file_put_contents($path, $generator-&gt;generate());
</pre>

    </div>

    <div class="example" id="zend.codegenerator.examples.reflection-class"><div class="info"><p><b>Example #6 Seeding PHP class generation via reflection</b></p></div>
        

        <div class="example-contents"><p>
            You may add code to an existing class. To do so, first use the
            static  <span class="methodname">fromReflection()</span> method to map the class into a
            generator object. From there, you may add additional properties or
            methods, and then regenerate the class.
        </p></div>

        <pre class="programlisting brush: php">
$generator = Zend_CodeGenerator_Php_Class::fromReflection(
    new Zend_Reflection_Class($class)
);
$generator-&gt;setMethod(array(
    &#039;name&#039;       =&gt; &#039;setBaz&#039;,
    &#039;parameters&#039; =&gt; array(
        array(&#039;name&#039; =&gt; &#039;baz&#039;),
    ),
    &#039;body&#039;       =&gt; &#039;$this-&gt;_baz = $baz;&#039; . &quot;\n&quot; . &#039;return $this;&#039;,
    &#039;docblock&#039;   =&gt; new Zend_CodeGenerator_Php_Docblock(array(
        &#039;shortDescription&#039; =&gt; &#039;Set the baz property&#039;,
        &#039;tags&#039;             =&gt; array(
            new Zend_CodeGenerator_Php_Docblock_Tag_Param(array(
                &#039;paramName&#039; =&gt; &#039;baz&#039;,
                &#039;datatype&#039;  =&gt; &#039;string&#039;
            )),
            new Zend_CodeGenerator_Php_Docblock_Tag_Return(array(
                &#039;datatype&#039;  =&gt; &#039;string&#039;,
            )),
        ),
    )),
));
$code = $generator-&gt;generate();
</pre>

    </div>
</div>
        <hr />

            <table width="100%">
                <tr>
                    <td width="25%" style="text-align: left;">
                    <a href="zend.codegenerator.introduction.html">Introduction</a>
                    </td>

                    <td width="50%" style="text-align: center;">
                        <div class="up"><span class="up"><a href="zend.codegenerator.html">Zend_CodeGenerator</a></span><br />
                        <span class="home"><a href="manual.html">Programmer's Reference Guide</a></span></div>
                    </td>

                    <td width="25%" style="text-align: right;">
                        <div class="next" style="text-align: right; float: right;"><a href="zend.codegenerator.reference.html">Zend_CodeGenerator Reference</a></div>
                    </td>
                </tr>
            </table>
</td>
        <td style="font-size: smaller;" width="15%"> <style type="text/css">
#leftbar {
	float: left;
	width: 186px;
	padding: 5px;
	font-size: smaller;
}
ul.toc {
	margin: 0px 5px 5px 5px;
	padding: 0px;
}
ul.toc li {
	font-size: 85%;
	margin: 1px 0 1px 1px;
	padding: 1px 0 1px 11px;
	list-style-type: none;
	background-repeat: no-repeat;
	background-position: center left;
}
ul.toc li.header {
	font-size: 115%;
	padding: 5px 0px 5px 11px;
	border-bottom: 1px solid #cccccc;
	margin-bottom: 5px;
}
ul.toc li.active {
	font-weight: bold;
}
ul.toc li a {
	text-decoration: none;
}
ul.toc li a:hover {
	text-decoration: underline;
}
</style>
 <ul class="toc">
  <li class="header home"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="manual.html">Programmer's Reference Guide</a></li>
  <li class="header up"><a href="reference.html">Zend Framework Reference</a></li>
  <li class="header up"><a href="zend.codegenerator.html">Zend_CodeGenerator</a></li>
  <li><a href="zend.codegenerator.introduction.html">Introduction</a></li>
  <li class="active"><a href="zend.codegenerator.examples.html">Zend_CodeGenerator Examples</a></li>
  <li><a href="zend.codegenerator.reference.html">Zend_CodeGenerator Reference</a></li>
 </ul>
 </td>
    </tr>
</table>

<script type="text/javascript" src="../js/shCore.js"></script>
<script type="text/javascript" src="../js/shAutoloader.js"></script>
<script type="text/javascript" src="../js/main.js"></script>

</body>
</html>